home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / mm / ccmd / cmtok.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-12-18  |  3.5 KB  |  121 lines

  1. /*
  2.  Copyright (c) 1986, 1990 by The Trustees of Columbia University in
  3.  the City of New York.  Permission is granted to any individual or
  4.  institution to use, copy, or redistribute this software so long as it
  5.  is not sold for profit, provided this copyright notice is retained.
  6.  
  7.  Author: Andrew Lowry
  8. */
  9. /* cmtok
  10. **
  11. ** This module contains handlers for parsing arbitrary token strings.
  12. ** A parse succeeds if the current input exactly matches the given
  13. ** token, with case insignificant in letters.  No terminating character
  14. ** is needed for a successful parse.  Completion succeeds if any
  15. ** prefix of the token has been typed.  Full completion adds a space
  16. ** after the token as well.  Partial completion will stop after the
  17. ** first punctuation char completed. Help text echoes the expected string.
  18. ** The standard break table breaks on every single character, 
  19. ** so CM_WKF parsing will work correctly (since no terminating
  20. ** character is required).
  21. **/
  22.  
  23. #define    TOKERR            /* token error table allocated here */
  24.  
  25. #include "ccmdlib.h"            /* get standard symbols */
  26. #include "cmfncs.h"        /* and internal symbols */
  27.  
  28. /* Forward declaration of handler routines */
  29.  
  30. int tokprs(), tokhlp(), tokcplt();
  31.         
  32. #define    tokbrk    cmallbk        /* std break table breaks on everything */
  33.  
  34. ftspec ft_tok = { tokprs, tokhlp, tokcplt, 0, &tokbrk };
  35.  
  36. /* tokprs
  37. **
  38. ** Purpose:
  39. **   Attempt to parse the given token string.  If the string is
  40. **   not completely present in the input, signal an incomplete
  41. **   parse.  If there are any mismatched characters, signal an
  42. **   error.
  43. **/
  44.  
  45. PASSEDSTATIC int
  46. tokprs(text,textlen,fdbp,parselen,value)
  47. char *text;
  48. int textlen;
  49. fdb *fdbp;
  50. int *parselen;
  51. pval *value;
  52. {
  53.   char *tok = (char *) fdbp->_cmdat;    /* point to token string */
  54.   char c1,c2;                /* chars in token and text */
  55.   int toklen;                /* length of token string */
  56.   
  57.   toklen = 0;                /* haven't parsed any chars yet */
  58.   while ((c1 = *tok++) != NULCHAR) {    /* loop to end of token string */
  59.     if (++toklen > textlen)        /* ran out of input? */
  60.       return(CMxINC);            /* ok, good match up to here */
  61.     c2 = (*text++) & CC_CHR;        /* get next input char */
  62.     if (islower(c1))            /* convert both chars to upper case */
  63.       c1 = toupper(c1);
  64.     if (islower(c2))
  65.       c2 = toupper(c2);
  66.     if (c1 != c2)
  67.       return(TOKxNP);            /* mismatch */
  68.   }
  69.   *parselen = toklen;            /* good match - set parse length */
  70.   value->_pvstr = cmcsb._cmabp;        /* return value in atom buffer */
  71.   if (toklen == textlen)
  72.       if (!(fdbp->_cmffl & TOK_WAK))
  73.       return(CMxINC);
  74.   return(CMxOK);
  75. }
  76.  
  77.  
  78.  
  79. /* tokhlp - Identify the string we're looking for */
  80.  
  81. PASSEDSTATIC int
  82. tokhlp(text,textlen,fdbp,cust, lines)
  83. char *text;
  84. int textlen,cust;
  85. fdb *fdbp;
  86. int lines;
  87. {
  88.   if (!cust)
  89.     cmxputs("matching token: ");    /* start the message if they didnt */
  90.   cmxputs((char *) fdbp->_cmdat);    /* then print the token string */
  91.   return(lines-1);
  92. }
  93.  
  94.  
  95.  
  96.  
  97. /* tokcplt - Finish off the token string for them.  Add a space if
  98. ** this is full completion.
  99. **/
  100.  
  101. PASSEDSTATIC int
  102. tokcplt(text,textlen,fdbp,full,cplt,cpltlen)
  103. char *text,**cplt;
  104. int textlen,full,*cpltlen;
  105. fdb *fdbp;
  106. {
  107.   char *tok = (char *) fdbp->_cmdat;    /* point to token string */
  108.   char c1,c2;                /* chars in token and text */
  109.   
  110.   if (textlen == 0) {
  111.     *cplt = NULL;            /* beep on no text */
  112.     return(CMP_BEL);
  113.   }
  114.   *cplt = tok + textlen;        /* set rest of token for completion */
  115.   *cpltlen = -1;            /* complete to end of string */
  116.   if (full)
  117.     return(CMP_SPC | CMP_GO);        /* add space and wakeup for full */
  118.   else
  119.     return(CMP_PNC);            /* only up to punct for partial */
  120. }
  121.